package generator.website;

import java.util.Iterator;
import org.eclipse.emf.common.util.EList;
import website.*;

public class WebSiteValidationFormFactory
{
  protected static String nl;
  public static synchronized WebSiteValidationFormFactory create(String lineSeparator)
  {
    nl = lineSeparator;
    WebSiteValidationFormFactory result = new WebSiteValidationFormFactory();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "package actionForms;" + NL + "" + NL + "import javax.servlet.http.HttpServletRequest;" + NL + "import org.apache.struts.action.ActionErrors;" + NL + "import org.apache.struts.action.ActionForm;" + NL + "import org.apache.struts.action.ActionMapping;" + NL + "import org.apache.struts.action.ActionMessage;" + NL + "import java.util.regex.Matcher;  " + NL + "import java.util.regex.Pattern; " + NL + "" + NL + "public class ";
  protected final String TEXT_2 = "ValidationForm extends ActionForm {\t" + NL + "\t" + NL + "\tprivate static final long serialVersionUID = 1L;";
  protected final String TEXT_3 = NL + "\tprivate String [] ";
  protected final String TEXT_4 = " = null;";
  protected final String TEXT_5 = NL + "\tprivate String ";
  protected final String TEXT_6 = " = null;";
  protected final String TEXT_7 = NL + "\tpublic String [] get";
  protected final String TEXT_8 = "(){" + NL + "\t\treturn this.";
  protected final String TEXT_9 = ";" + NL + "\t}" + NL + "\t" + NL + "\tpublic void set";
  protected final String TEXT_10 = "(String [] text){" + NL + "\t\tthis.";
  protected final String TEXT_11 = " = text;" + NL + "\t}";
  protected final String TEXT_12 = NL + "\t" + NL + "\tpublic String get";
  protected final String TEXT_13 = "(){" + NL + "\t\treturn this.";
  protected final String TEXT_14 = ";" + NL + "\t}" + NL + "\t" + NL + "\tpublic void set";
  protected final String TEXT_15 = "(String text){" + NL + "\t\tthis.";
  protected final String TEXT_16 = " = text;" + NL + "\t}";
  protected final String TEXT_17 = NL + "\t" + NL + "\tpublic void reset(ActionMapping mapping, HttpServletRequest request) {" + NL + "\t\t";
  protected final String TEXT_18 = NL + "\t\tthis.";
  protected final String TEXT_19 = " = null;";
  protected final String TEXT_20 = NL + "\t}" + NL + "\t" + NL + "\tpublic ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { " + NL + "\t\t\tActionErrors errors = new ActionErrors();" + NL + "\t    ";
  protected final String TEXT_21 = NL + "\t\t\tString expression = \"^[0-9]{2}-[0-9]{2}-[0-9]{4}$\";" + NL + "\t\t\tif(!get";
  protected final String TEXT_22 = ".matches(expression))" + NL + "   \t\t\t\terrors.add(\"";
  protected final String TEXT_23 = "\", new ActionMessage(\"";
  protected final String TEXT_24 = ".";
  protected final String TEXT_25 = ".error.required\"));\t\t" + NL + "\t\t\t\t";
  protected final String TEXT_26 = NL + "\t\t\tString expression = \"^[\\\\w\\\\-]+(\\\\.[\\\\w\\\\-]+)*@([A-Za-z0-9-]+\\\\.)+[A-Za-z]{2,4}$\";" + NL + "\t\t\tPattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);  " + NL + "   \t\t\tMatcher matcher = pattern.matcher(get";
  protected final String TEXT_27 = "());  " + NL + "   \t\t\tif(!matcher.matches())" + NL + "   \t\t\t\terrors.add(\"";
  protected final String TEXT_28 = "\", new ActionMessage(\"";
  protected final String TEXT_29 = ".";
  protected final String TEXT_30 = ".error.required\"));\t\t" + NL + "\t\t\t\t";
  protected final String TEXT_31 = NL + "\t\t\tif(get";
  protected final String TEXT_32 = "() == null || get";
  protected final String TEXT_33 = "().length() < 1){" + NL + "\t\t\t\terrors.add(\"";
  protected final String TEXT_34 = "\", new ActionMessage(\"";
  protected final String TEXT_35 = ".";
  protected final String TEXT_36 = ".error.required\"));" + NL + "\t\t\t}\t\t\t" + NL + "\t\t\t";
  protected final String TEXT_37 = " " + NL + "\t    return errors;" + NL + "\t}" + NL + "} ";

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    Form form = (Form) argument;
    stringBuffer.append(TEXT_1);
    stringBuffer.append(form.getType().getName());
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_2);
    
	EList<Tag> Listtag = form.getTags();
	for(Tag tag : Listtag){
		if(tag instanceof FormTag && !(tag instanceof Button)){
			if(tag instanceof Select || tag instanceof RadioButton){
    stringBuffer.append(TEXT_3);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_4);
    
		} else
    stringBuffer.append(TEXT_5);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_6);
    
		}
	}	
	for(Tag tag : Listtag){
		if(tag instanceof FormTag && !(tag instanceof Button)){
			if(tag instanceof Select || tag instanceof RadioButton){
    stringBuffer.append(TEXT_7);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_8);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_9);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_10);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_11);
    
			} else{
    stringBuffer.append(TEXT_12);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_13);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_14);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_15);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_16);
    
			}
		}
	}
    stringBuffer.append(TEXT_17);
    for(Tag tag : Listtag){
			if(tag instanceof FormTag && !(tag instanceof Button)){
    stringBuffer.append(TEXT_18);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_19);
    
			}
		}
    stringBuffer.append(TEXT_20);
    for(Tag tag : Listtag){
			if(tag instanceof FormTag && !(tag instanceof Button) && ((FormTag) tag).isRequired()){
				if(tag instanceof Date){
				
    stringBuffer.append(TEXT_21);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_22);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_23);
    stringBuffer.append(form.getType().getName());
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_24);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_25);
    
				} else if(tag instanceof TextBox && ((TextBox)tag).getType() == TextType.getByName("email")){
				
    stringBuffer.append(TEXT_26);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_27);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_28);
    stringBuffer.append(form.getType().getName());
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_29);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_30);
    
				} else {
			
    stringBuffer.append(TEXT_31);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_32);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_33);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_34);
    stringBuffer.append(form.getType().getName());
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_35);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_36);
    	}
			}
		}
    stringBuffer.append(TEXT_37);
    return stringBuffer.toString();
  }
}
